package com.chart.chart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chart.chart.domian.ChartConfigsUserNew;
import com.chart.chart.domian.ChartFilesNew;
import com.chart.chart.mapper.ChartConfigsUserNewMapper;
import com.chart.chart.service.ChartFilesNewService;
import com.chart.common.dto.ChartConfigUserDTO;
import com.chart.common.dto.ChartFileTreeDTO;
import com.chart.common.utils.SecurityUtils;
import com.chart.system.mapper.ChartFilesNewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * ChartFilesNew 服务实现类
 */
@Service
public class ChartFilesNewServiceImpl extends ServiceImpl<ChartFilesNewMapper, ChartFilesNew> implements ChartFilesNewService {

    @Autowired
    private ChartFilesNewMapper chartFilesNewMapper;
    @Autowired
    private ChartConfigsUserNewMapper chartConfigsUserNewMapper;

    @Override
    public List<ChartFileTreeDTO> getFolderFileTree(String name) {
        Long userId = SecurityUtils.getUserId(); // 获取用户ID

        // 查询所有文件夹和文件
        List<ChartFilesNew> allFolders = chartFilesNewMapper.selectFilesByName(name, userId);
        List<ChartConfigsUserNew> allFiles = chartConfigsUserNewMapper.selectConfigsByName(name, userId);

        if (name != null) {
            // 按名称筛选匹配的文件夹和文件
            List<ChartFileTreeDTO> matchedFolders = allFolders.stream()
                    .filter(folder -> folder.getFileName() != null && folder.getFileName().contains(name))
                    .map(this::convertToDTO)
                    .collect(Collectors.toList());
            List<ChartConfigUserDTO> matchedFiles = allFiles.stream()
                    .filter(file -> file.getChartName() != null && file.getChartName().contains(name))
                    .map(this::convertFileToDTO)
                    .collect(Collectors.toList());

            if (!matchedFolders.isEmpty() || !matchedFiles.isEmpty()) {
                ChartFileTreeDTO matchedNode = new ChartFileTreeDTO();
                matchedNode.setFileName("我的生成");
                matchedNode.setChildren(matchedFolders);
                matchedNode.setFiles(matchedFiles);
                return Arrays.asList(matchedNode);
            }
        }

        // 转换文件夹和文件为 DTO
        List<ChartFileTreeDTO> folderDTOList = allFolders.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        List<ChartConfigUserDTO> fileDTOList = allFiles.stream()
                .map(this::convertFileToDTO)
                .collect(Collectors.toList());

        // 按文件夹分组
        Map<Long, List<ChartConfigUserDTO>> filesByFolderId = fileDTOList.stream()
                .filter(file -> file.getFileId() != null)
                .collect(Collectors.groupingBy(ChartConfigUserDTO::getFileId));

        // 获取根目录文件
        List<ChartConfigUserDTO> rootFiles = fileDTOList.stream()
                .filter(file -> file.getFileId() == null)
                .collect(Collectors.toList());

        // 构建文件夹树
        List<ChartFileTreeDTO> rootFolders = buildTree(folderDTOList, filesByFolderId, null);

        // 构建根节点
        ChartFileTreeDTO rootNode = new ChartFileTreeDTO();
        rootNode.setFileName("我的生成");
        rootNode.setFiles(rootFiles);
        rootNode.setChildren(rootFolders);

        return Arrays.asList(rootNode);
    }

    @Override
    public List<ChartFileTreeDTO> getFolderFileTreeNew(String name) {

        Long userId = SecurityUtils.getUserId(); // 获取用户ID

        // 查询所有文件夹和文件
        List<ChartFilesNew> allFolders = chartFilesNewMapper.selectFilesByNameNew(name, userId);
        List<ChartConfigsUserNew> allFiles = chartConfigsUserNewMapper.selectConfigsByNameNew(name, userId);

        if (name != null) {
            // 按名称筛选匹配的文件夹和文件
            List<ChartFileTreeDTO> matchedFolders = allFolders.stream()
                    .filter(folder -> folder.getFileName() != null && folder.getFileName().contains(name))
                    .map(this::convertToDTO)
                    .collect(Collectors.toList());
            List<ChartConfigUserDTO> matchedFiles = allFiles.stream()
                    .filter(file -> file.getChartName() != null && file.getChartName().contains(name))
                    .map(this::convertFileToDTO)
                    .collect(Collectors.toList());

            if (!matchedFolders.isEmpty() || !matchedFiles.isEmpty()) {
                ChartFileTreeDTO matchedNode = new ChartFileTreeDTO();
                matchedNode.setFileName("我的生成");
                matchedNode.setChildren(matchedFolders);
                matchedNode.setFiles(matchedFiles);
                return Arrays.asList(matchedNode);
            }
        }

        // 转换文件夹和文件为 DTO
        List<ChartFileTreeDTO> folderDTOList = allFolders.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        List<ChartConfigUserDTO> fileDTOList = allFiles.stream()
                .map(this::convertFileToDTO)
                .collect(Collectors.toList());

        // 按文件夹分组
        Map<Long, List<ChartConfigUserDTO>> filesByFolderId = fileDTOList.stream()
                .filter(file -> file.getFileId() != null)
                .collect(Collectors.groupingBy(ChartConfigUserDTO::getFileId));

        // 获取根目录文件
        List<ChartConfigUserDTO> rootFiles = fileDTOList.stream()
                .filter(file -> file.getFileId() == null)
                .collect(Collectors.toList());

        // 构建文件夹树
        List<ChartFileTreeDTO> rootFolders = buildTree(folderDTOList, filesByFolderId, null);

        // 构建根节点
        ChartFileTreeDTO rootNode = new ChartFileTreeDTO();
        rootNode.setFileName("我的生成");
        rootNode.setFiles(rootFiles);
        rootNode.setChildren(rootFolders);

        return Arrays.asList(rootNode);
    }









    private ChartFileTreeDTO convertToDTO(ChartFilesNew chartFile) {
        ChartFileTreeDTO dto = new ChartFileTreeDTO();
        dto.setFileId(chartFile.getFileId());
        dto.setFileName(chartFile.getFileName());
        dto.setParentId(chartFile.getParentId());
        return dto;
    }

    private ChartConfigUserDTO convertFileToDTO(ChartConfigsUserNew chartConfigUserNew) {
        ChartConfigUserDTO dto = new ChartConfigUserDTO();
        dto.setId(chartConfigUserNew.getId());
        dto.setChartName(chartConfigUserNew.getChartName());
        dto.setFileId(chartConfigUserNew.getFileId());
        dto.setChartValue(chartConfigUserNew.getChartValue());
        dto.setImageUrl(chartConfigUserNew.getImageUrl());
        return dto;
    }

    private List<ChartFileTreeDTO> buildTree(List<ChartFileTreeDTO> folderList,
                                             Map<Long, List<ChartConfigUserDTO>> filesByFolderId,
                                             Long parentId) {
        return folderList.stream()
                .filter(folder -> (parentId == null && folder.getParentId() == null) ||
                        (parentId != null && parentId.equals(folder.getParentId())))
                .map(folder -> {
                    if (parentId == null) {
                        List<ChartConfigUserDTO> rootFiles = filesByFolderId.get(null);
                        if (rootFiles != null) {
                            folder.setFiles(rootFiles);
                        }
                    }
                    folder.setFiles(filesByFolderId.get(folder.getFileId()));
                    folder.setChildren(buildTree(folderList, filesByFolderId, folder.getFileId()));
                    return folder;
                })
                .collect(Collectors.toList());
    }

















}
